package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

/**
 * @author 陶贞
 * @version 1.0
 */
//登录页面UI自动化测试
public class LoginPage extends Utils {
	//login页面的url
	public static String url = "http://localhost:8080/blog_login.html";
	public LoginPage() {
		super(url);
	}

	//检查页面是否加载成功
	public  void loginPageLoad(){
		//查找页面元素
		driver.findElement(By.cssSelector("body > div.container-login > div"));
		driver.findElement(By.cssSelector("#submit"));
	}

	//登录成功
	public  void loginSuccess(){
		//输入正确用户名和密码
		driver.findElement(By.cssSelector("#username")).sendKeys("桃子");
		driver.findElement(By.cssSelector("#password")).sendKeys("123");
		driver.findElement(By.cssSelector("#submit")).click();
		//查找注销按钮检查是否登录成功
		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
		//注销回到登录页面（为了测试用例的有序执行，此处就不返回登录页面了，等登录状态下最好一个用例执行完毕再注销）
//		driver.navigate().back();
//		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
	}

	//登录失败
	public  void loginFail() throws InterruptedException, IOException {
		driver.findElement(By.cssSelector("#username")).clear();
		driver.findElement(By.cssSelector("#password")).clear();
		//刷新清除输入框内容
//		driver.navigate().refresh();
		//输入错误用户名和密码
		driver.findElement(By.cssSelector("#username")).sendKeys("lisi");
		driver.findElement(By.cssSelector("#password")).sendKeys("123");
		driver.findElement(By.cssSelector("#submit")).click();


		Thread.sleep(100);
		//点击警告弹窗
		Alert alert = driver.switchTo().alert();
		alert.accept();

		//刷新页面清空输入框内容
		driver.navigate().refresh();

		//账号密码为空
		driver.findElement(By.cssSelector("#submit")).click();
		Thread.sleep(100);
		alert.accept();
	}
}
